package com.seagox.chat.chat.controller;

import com.seagox.chat.chat.entity.BaseUser;
import com.seagox.chat.chat.service.IUserService;
import com.seagox.chat.chat.verify.UserExcelVerifyHandler;
import com.seagox.chat.common.ResultCode;
import com.seagox.chat.common.ResultData;
import com.seagox.chat.template.UserModel;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 用户
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;
    
    @Autowired
	private UserExcelVerifyHandler userExcelVerifyHandler;

    /**
     * 查询通过部门id
     */
    @GetMapping("/queryByDepartId")
    public ResultData queryByDepartId(String departId) {
        return userService.queryByDepartId(departId);
    }
    
    /**
     * 添加
     */
    @PostMapping("/insert")
    public ResultData insert(BaseUser user) {
        return userService.insert(user);
    }
    
    /**
     * 编辑
     */
    @PostMapping("/edit")
    public ResultData edit(BaseUser user) {
        return userService.edit(user);
    }
    
    /**
     * 更新
     */
    @PostMapping("/update")
    public ResultData update(BaseUser user) {
        return userService.update(user);
    }
    
    /**
	 * 修改密码
	 *
	 * @param userId      用户id
	 * @param oldPassword 旧密码
	 * @param newPassword 新密码
	 */
	@PostMapping("/updatePassword")
	public ResultData updatePassword(String userId, String oldPassword, String newPassword) {
		return userService.updatePassword(userId, oldPassword, newPassword);
	}
	
	/**
	 * 更新头像
	 */
	@PostMapping("/updateAvatar")
	public ResultData updateAvatar(String userId, String avatar) {
		return userService.updateAvatar(userId, avatar);
	}
	
	/**
	 * 删除
	 */
	@PostMapping("/delete")
	public ResultData delete(String ids) {
		return userService.delete(ids);
	}
	
	/**
	 * 重置密码
	 */
	@PostMapping("/resetPassword")
	public ResultData resetPassword(String id) {
		return userService.resetPassword(id);
	}
	
	/**
	 * 导入
	 */
	@PostMapping("/import")
	public ResultData importHandle(@RequestParam("file") MultipartFile file) {
		ImportParams params = new ImportParams();
		params.setHeadRows(1);
		params.setNeedVerify(true);
		params.setVerifyHandler(userExcelVerifyHandler);// 设置一个验证处理器
		try {
			ExcelImportResult<UserModel> result = ExcelImportUtil.importExcelMore(file.getInputStream(),
					UserModel.class, params);
			Map<String, Object> repeatMap = new HashMap<>();
			List<UserModel> resultList = result.getList();
			for (int i = 0; i < resultList.size(); i++) {
				if (repeatMap.containsKey(resultList.get(i).getPhone())) {
					return ResultData.warn(ResultCode.OTHER_ERROR, "手机号重复,请检查:" + resultList.get(i).getPhone());
				} else {
					repeatMap.put(resultList.get(i).getPhone(), 1);
				}
			}
			// 判断是否有错误
			if (result.isVerifyFail()) {
				for (UserModel entity : result.getFailList()) {
					return ResultData.warn(ResultCode.OTHER_ERROR,
							"第" + entity.getRowNum() + "行的错误是：" + entity.getErrorMsg());
				}
			} else {
				// 获到正确的数据
				userService.importHandle(result.getList());
			}
			return ResultData.success(null);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultData.warn(ResultCode.INTERNAL_SERVER_ERROR, e.getMessage());
		}

	}

}

